package com.suneee.base.util;

import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;

import com.suneee.base.context.OnlineUser;
import com.suneee.base.context.Session;
import com.suneee.context.util.SpringContextHelper;
import com.suneee.sso.admin.entity.Administrator;

public class ContextHelper {

	// 保存用户登录的redis对象
	private static RedisTemplate<String, Administrator> loginRedis = null;
	
	private static RedisTemplate<String,String> identityStrRedis = null;

	/**
	 * 将管理员登录信息保存到redis中
	 * @param admin
	 */
	public static void setLoginInfo(Administrator admin) {
		// 默认1天
		int time = 29;
		initLoginRedis().opsForValue().set(String.valueOf(admin.getAdminId()), admin, time, TimeUnit.MINUTES);

		String userId = String.valueOf(admin.getAdminId());
		String key = "t_administrator"+"_"+"admin_id"+"user_id"+userId;
		 
		loginRedis.opsForSet().add(key, admin);
		
	}
	
	/**
	 * 邮件标识
	 * @param admin
	 */
	public static void setIdentityStr(Administrator admin){
		delteIdentityStr(admin.getAdminId().toString());
		int time = 29;
		initStrRedis().opsForValue().set(String.valueOf("identity"+admin.getAdminId()), admin.getIdentityStr(), time, TimeUnit.MINUTES);
//		System.out.println(initStrRedis().opsForValue().get("identity"+String.valueOf(admin.getAdminId())));
//		String userId = String.valueOf(admin.getAdminId());
//		String key = "identity"+userId;
//		identityStrRedis.opsForSet().add(key, admin.getIdentityStr());
	}
	
	/**
	 * 获取邮件标识
	 * @param key
	 * @return
	 */
	public static String getIdentityStr(String key){
		String getKey = "identity"+key;
		return initStrRedis().opsForValue().get(getKey);
	}
	
	/**
	 * 删除邮件标识
	 * @param adminId
	 */
	public static void delteIdentityStr(String adminId) {
		String delKey = "identity"+adminId;
		initStrRedis().delete(delKey);
	}
	
	/**
	 * 删除管理员的登录信息
	 * @param userId
	 */
	public static void delteLoginInfo(String key) {
		String delKey = "t_administrator"+"_"+"admin_id"+"user_id"+key;
		initLoginRedis().delete(delKey);
	}
	
	/**
	 * 获取管理员的登录信息
	 * redis
	 * @param userId
	 * @return
	 */
	public static Administrator getLoginInfo(String key){
		String getKey = "t_administrator"+"_"+"admin_id"+"user_id"+key;
		return initLoginRedis().opsForValue().get(getKey);
	}
	
	/**
	 * 获取当前登录用户信息
	 * @return
	 */
	public static Administrator getCurrentLoginInfo(){
//		HttpServletRequest request = (HttpServletRequest) ThreadLocalCache.getObject(UserFilter.REQUEST);
//		HttpSession session = request.getSession();
//		Administrator admin = (Administrator) session.getAttribute("LOGINUSER");
		
		Session session = UserThreadLocal.getSession();
		OnlineUser onlineUser = session.getOnlineUser();
		Administrator admin = onlineUser.getAdmin();
		return admin;
	}

	@SuppressWarnings("unchecked")
	private static RedisTemplate<String, Administrator> initLoginRedis() {
		if (null == loginRedis) {
			loginRedis = getBean("redisTemplate", RedisTemplate.class);
		}
		return loginRedis;
	}
	
	@SuppressWarnings("unchecked")
	private static RedisTemplate<String, String> initStrRedis() {
		if (null == identityStrRedis) {
			identityStrRedis = getBean("redisTemplate", RedisTemplate.class);
		}
		return identityStrRedis;
	}
	
	public static <T> T getBean(String name, Class<T> cls){
		return SpringContextHelper.getContext().getBean(name, cls);
	}
}
